package com.club.football.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.football.dto.PageDto;
import com.club.football.entity.Events;
import com.club.football.entity.EventsTeams;
import com.club.football.entity.Team;
import com.club.football.mapper.EventsTeamsMapper;
import com.club.football.req.EventsTeamsReq;
import com.club.football.service.EventsService;
import com.club.football.service.EventsTeamsService;
import com.club.football.service.TeamService;
import com.club.football.utils.BeanUtils;
import com.club.football.utils.R;
import com.club.football.vo.EventsTeamsListVo;
import com.club.football.vo.EventsTeamsVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.club.football.common.CommonConstants.GET_INFO_OK;

/**
 * 参赛球队服务实现
 *
 * @author 1
 * @date 2024/01/09
 */
@Service
public class EventsTeamsServiceImpl extends ServiceImpl<EventsTeamsMapper, EventsTeams> implements EventsTeamsService {

    @Resource
    private TeamService teamService;

    @Resource
    private EventsService eventsService;


    @Override
    public R<Page<EventsTeamsVo>> eventsTeamsPage(PageDto<EventsTeams> pageDto, EventsTeamsReq eventsTeamsReq) {
        lambdaQuery().eq(eventsTeamsReq.getEventsId() != null, EventsTeams::getEventsId, eventsTeamsReq.getEventsId())
                .in(CollUtil.isNotEmpty(eventsTeamsReq.getTeamIdList()), EventsTeams::getTeamId, eventsTeamsReq.getTeamIdList())
                .page(pageDto);
        Page<EventsTeamsVo> eventsTeamsVoPage = new Page<>();
        eventsTeamsVoPage.setTotal(pageDto.getTotal());
        List<EventsTeams> records = pageDto.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            List<Long> teamIdList = records.stream().map(EventsTeams::getTeamId).collect(Collectors.toList());
            Map<Long, String> teamMap = teamService.lambdaQuery().in(Team::getTeamId, teamIdList)
                    .list().stream().collect(Collectors.toMap(Team::getTeamId, Team::getName));
            List<EventsTeamsVo> result = records.stream().map(e -> {
                EventsTeamsVo eventsTeamsVo = BeanUtils.copier(e, new EventsTeamsVo());
                eventsTeamsVo.setTeamName(teamMap.get(e.getTeamId()));
                return eventsTeamsVo;
            }).collect(Collectors.toList());
            eventsTeamsVoPage.setRecords(result);
        }
        return R.ok(eventsTeamsVoPage, GET_INFO_OK);
    }

    @Override
    public R<EventsTeamsListVo> eventsTeamsList(EventsTeamsReq eventsTeamsReq) {
        List<EventsTeams> eventsTeamsList = lambdaQuery().eq(eventsTeamsReq.getEventsId() != null, EventsTeams::getEventsId, eventsTeamsReq.getEventsId())
                .eq(CollUtil.isNotEmpty(eventsTeamsReq.getTeamIdList()), EventsTeams::getTeamId, eventsTeamsReq.getTeamIdList()).list();
        EventsTeamsListVo eventsTeamsListVo = new EventsTeamsListVo();
        if (CollUtil.isNotEmpty(eventsTeamsList)) {
            List<Long> teamIdList = eventsTeamsList.stream().map(EventsTeams::getTeamId).collect(Collectors.toList());
            List<Team> teamList = teamService.lambdaQuery().in(Team::getTeamId, teamIdList).list();
            eventsTeamsListVo.setTeamList(teamList);
            List<Long> eventsIdList = eventsTeamsList.stream().map(EventsTeams::getEventsId).collect(Collectors.toList());
            List<Events> eventsList = eventsService.lambdaQuery().in(Events::getEventsId, eventsIdList).list();
            eventsTeamsListVo.setEventsList(eventsList);
        }
        return R.ok(eventsTeamsListVo, GET_INFO_OK);
    }
}
